// views/index/shop/translationcopy/ordermessage/index.js
var util = require('../../../../../utils/util');
var BASE_URL = getApp().globalData.BASE_URL
import Notify from '../../../../../miniprogram_npm/@vant/weapp/notify/notify';
import Dialog from '../../../../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    minDate:new Date(1950,0,1).getTime(),
    value: '',
    openjiudupop: false,
    showdatepop: false,
    currentDate: new Date().getTime(),
    nowtime:"",
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    user_name: '',
    spell: '',
    sex: 1,
    born: "yyyy/mm/日",
    tel: '',
    wechat: '',
    location: '',
    jiudu: '',
    zhuanye: '',
    du_time: 'yyyy/mm/日',
    education: '',
    use: "申请签证",
    apply: '商科',
    language: '德语',
    beizhu: '',
    email: ""
  },

  //确定时间
  change_date: function () {
    this.setData({
      showdatepop: false,
      born: this.changeTime(this.data.nowtime)
    })
  },
  change_jiudu: function () {
    this.setData({
      openjiudupop: false,
      du_time: this.changeTime(this.data.nowtime)
    })
  },
  //切换时间
  getColumnValue(event) {
    console.log(event)
    this.setData({
      nowtime: event.detail,
    });
  },

  //打开时间pop
  opendatepop: function () {
    this.setData({
      showdatepop: true
    })
  },
  openjiudupop: function () {
    this.setData({
      openjiudupop: true
    })
  },
  //关闭时间pop
  onClose: function () {
    this.setData({
      showdatepop: false
    })
  },
  //用途切换
  change_use: function (e) {
    this.setData({
      use: e.currentTarget.dataset.use
    })
  },
  //领域切换
  change_apply: function (e) {
    this.setData({
      apply: e.currentTarget.dataset.apply
    })
  },
  //男女切换
  change_sex: function (e) {
    this.setData({
      sex: e.currentTarget.dataset.sex
    })
  },


  //立即办理
  placeorder: function () {
    var _this = this
    var userinfo = wx.getStorageSync("USER")

    if (!_this.ifhanzi(_this.data.user_name)) {
      Notify('姓名输入错误');
    }
    if (!_this.ifenglish(_this.data.spell)) {
      Notify('姓名拼音输入错误');
    }
    if (_this.data.location == "") {
      Notify('联系地址不能为空');
    }
    if (!_this.codeVerification(_this.data.tel)) {
      Notify('请输入正确的手机号');
    }

    if (_this.ifhanzi(_this.data.user_name) && _this.ifenglish(_this.data.spell) && _this.codeVerification(_this.data.tel) && _this.data.location !== "") {
      wx.request({
        url: `${BASE_URL}/fanyi/wenshudata`,
        method: "POST",
        data: {
          user_name: _this.data.user_name,
          spell: _this.data.spell,
          sex: _this.data.sex,
          born: _this.data.born,
          tel: _this.data.tel,
          wechat: _this.data.wechat,
          location: _this.data.location,
          jiudu: _this.data.jiudu,
          zhuanye: _this.data.zhuanye,
          du_time: _this.data.du_time,
          education: _this.data.education,
          use: _this.data.use,
          apply: _this.data.apply,
          language: _this.data.language,
          beizhu: _this.data.beizhu,
          email: _this.data.email,
          uid: userinfo.id,
          type: 'xiaochengxv'
        },
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          console.log(res)
          wx.navigateTo({
            url: `./placeanorderdetail/index?id=${res.data.id}`
          })
        }
      })

    }
  },


















  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var userinfo = wx.getStorageSync("USER")
    this.setData({
      email: userinfo.email
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  //手机号正则
  codeVerification: function (phone) {
    let phoneCodeVerification = /^[1][3,4,5,7,8][0-9]{9}$/;
    return phoneCodeVerification.test(phone);
  },
  //英文字母正则
  ifenglish: function (pinyin) {
    let ifenglish = /^[A-Za-z]+$/;
    return ifenglish.test(pinyin);
  },
  //汉字正则
  ifhanzi: function (pinyin) {
    let ifhanzi = /^[\u4e00-\u9fa5]+$/;
    return ifhanzi.test(pinyin);
  },
  //微信号正则
  ifwewhat: function (pinyin) {
    let ifwewhat = /^[0-9a-zA-Z]*$/g;
    return ifwewhat.test(pinyin);
  },
  //邮箱验证
  ifemail: function (email) {
    let ifemail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    return ifemail.test(email);
  },









  // 时间戳转换
  changeTime(time) {
    if (time) {
      var oDate = new Date(time * 1),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth() + 1,
        oDay = oDate.getDate(),
        oTime = oYear + "/" + this.getBz(oMonth) + "/" + this.getBz(oDay)
      return oTime;
    } else {
      return "";
    }
  },
  // 补0
  getBz(num) {
    if (parseInt(num) < 10) {
      num = "0" + num;
    }
    return num;
  },


})